﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace robotGUI
{
    public partial class ValueBar : UserControl
    {
        public enum ORIENTATION { Horizontal, Vertical };
        ORIENTATION _orientation;
        int _minimum, _maximum = 100, _value;
        Color _leftCol = Color.Aqua, _rightCol = Color.Blue;

        public ORIENTATION Orientation { get { return _orientation; } set { _orientation = value; Refresh(); } }
        public int Minimum { get { return _minimum; } set { _minimum = value; } }
        public int Maximum { get { return _maximum; } set { _maximum = value; } }
        public int Value { get { return _value; } set { if (_value != value) { _value = value; Refresh(); } } }
        public Color LeftColor { get { return _leftCol; } set { if (_leftCol != value) { _leftCol = value; Refresh(); } } }
        public Color RightColor { get { return _rightCol; } set { if (_rightCol != value) { _rightCol = value; Refresh(); } } }

        public ValueBar()
        {
            InitializeComponent();
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            using (Pen pen = new Pen(SystemColors.ActiveBorder, 1))
            {
                e.Graphics.DrawRectangle(pen,
                    0, 0, pictureBox1.ClientSize.Width - 1, pictureBox1.ClientSize.Height - 1);
            }
            float range = (_maximum - _minimum);
            float x, y, wide, high;
            Point gradient;
            if (Orientation == ORIENTATION.Horizontal)
            {
                // Calculate the needle's Y coordinate.
                x = 1; 
                wide = (_value - _minimum) / range * (pictureBox1.ClientSize.Width - 2);
                y = pictureBox1.ClientSize.Height * 0.2F;
                high = pictureBox1.ClientSize.Height - 2 * y;
                gradient = new Point(0, pictureBox1.ClientSize.Height);
            }
            else
            {
                // Calculate the needle's X coordinate.
                y = (pictureBox1.ClientSize.Height - 2) - ((_value - _minimum) / range * (pictureBox1.ClientSize.Height - 2));
                x = pictureBox1.ClientSize.Width * 0.2F;
                wide = pictureBox1.ClientSize.Width - 2 * x;
                high = pictureBox1.ClientSize.Height - (int)y - 1;
                gradient = new Point(pictureBox1.ClientSize.Width, 0);
            }
            // Draw it.
            using (LinearGradientBrush brush = new LinearGradientBrush(new Point(0, 0), gradient, _leftCol, _rightCol))
            {
                e.Graphics.FillRectangle(brush,
                    x, y, wide, high);
            }
            using (Pen pen = new Pen(_rightCol, 3))
            {
                if (Orientation == ORIENTATION.Horizontal)
                    e.Graphics.DrawLine(pen, wide, 0, wide, pictureBox1.ClientSize.Height - 1);
                else
                    e.Graphics.DrawLine(pen, 0, y, pictureBox1.ClientSize.Width - 1, y);
            }
        }

        private void pictureBox1_Resize(object sender, EventArgs e)
        {
            this.Refresh();
        }

    }
}
